import { reqGoodsDetail } from '@/api'
const state = {
  goodInfo: {}
}
const mutations = {
  GETGOODSDETAIL(state, goodInfo) {
    state.goodInfo = goodInfo
  }
}
const actions = {
  // 获取详情的action
  async getGoodsDetail({ commit }, skuId) {
    // 等待请求完成
    const result = await reqGoodsDetail(skuId)
    if (result.code === 200) {
      // 提交mutation
      commit('GETGOODSDETAIL', result.data)
    }
  }
}
const getters = {
  categoryView(state) {
    // 如果goodInfo没有值时，返回空对象，避免报错
    return state.goodInfo.categoryView || {}
  },
  skuInfo(state) {
    return state.goodInfo.skuInfo || {}
  },
  spuSaleAttrList(state) {
    return state.goodInfo.spuSaleAttrList || {}
  }
}
export default {
  state,
  mutations,
  actions,
  getters
}
